import { createStore } from "vuex";

export default createStore({
  // 声明变量,
  state: {
    // 登录用户
    loginUser: null,
    // 当前登录状态，默认false
    isLogin: false,
    // token
    accessToken: "",
    refreshToken: "",
    // 保存socket实例 以供其他页面组件来使用
    socket: null,
    // 保存webrtc 实例对象
    webrtc: null,
    // 保存当前概览，全局使用
    imPendingOverview: null,
    // webrtc offer 信息
    webrtcOfferMessage: null,

    // 最大延迟接听时间，setTimeout 的返回值
    maxDelayReceiveTime: null,



    // ===========================================会议聊天客户端=====================================
    roomClient: null,
    // room 对象
    room: {
      // roomId 唯一标识
      id: null,
      // 房间名称
      roomName: null,
      // 该客户端连接房间状态：new/connecting/connected/disconnected/closed,
      roomState: 'new',
      // 房间中自己的属性
      me: {
        // peerId
        peerId: null,
        // 展示名称
        displayName: null,
        // 浏览器信息
        device: null,
        // 生产者mediasoup Producer对象(该客户端的生产者，音频或视频，一般两个)
        producers: [],
        // 消费者 Consumer peer 对象(其他端的生产者作为消费者被该客户端消费)
        consumerPeers: []
      },

    },

  },
  // 修改变量（state不能直接赋值修改，只能通过mutations）
  mutations: {
    login (state, accessToken, refreshToken) {
      state.isLogin = true;
      state.accessToken = accessToken;
      state.refreshToken = refreshToken;
    },
    // 设置socket
    setSocket (state, socket) {
      state.socket = socket;
    },
    // 设置webrtc
    setWebrtc (state, webrtc) {
      state.webrtc = webrtc;
    },
    setWebrtcOfferMessage (state, offerMessage) {
      state.webrtcOfferMessage = offerMessage;
    },
    setMaxDelayReceiveTime (state, maxDelayReceiveTime) {
      state.maxDelayReceiveTime = maxDelayReceiveTime;
    },
    setLoginUser (state, loginUser) {
      state.loginUser = loginUser;
    },
    setAccessToken (state, accessToken) {
      state.accessToken = accessToken;
    },
    setRefreshToken (state, refreshToken) {
      state.refreshToken = refreshToken;
    },
    setImPendingOverview (state, imPendingOverview) {
      state.imPendingOverview = imPendingOverview;
    },
    // 清除全部缓存
    clear (state) {
      state.loginUser = null;
      state.isLogin = false;
      state.accessToken = "";
      state.refreshToken = "";
      state.socket = null;
    },

    // 设置会议聊天客户端
    setRoomClient (state, roomClient) {
      state.roomClient = roomClient;

    },
    // 设置客户端当前连接房间的状态
    setRoomState (state, status) {
      state.room.roomState = status
    },
    // 初始化房间中其他人的信息
    addPeer (state, consumerPeer) {
      // 循环peers进行赋值处理
      state.room.me.consumerPeers.push(consumerPeer);
      console.info(state.room.me.consumerPeers);
    },
    // 添加生产者
    addProducer (state, producer) {
      state.room.me.producers.push(producer);
    },
    // 移除生产者
    removeProducer (state, producerId) {
      state.room.me.producers.forEach((item, index, arr) => {
        if (item.id === producerId) {
          arr.splice(index, 1);
        }
      });
    },
    // 添加消费者
    addConsumer (state, { consumer, peerId }) {
      // 肯定先添加peer 在逐个添加消费者

      state.room.me.consumerPeers.forEach((item, index, arr) => {
        if (item.peerId === peerId) {
          item.consumers.push(consumer);
        }
      });
      // 打印出来看下
      console.info(state.room.me.consumerPeers);
    },
    // 是否是只开启音频
    isAudioOnly (state) {
      return false;
    },
    // 移除peer
    removePeer (state, { peerId }) {
      state.room.me.consumerPeers.forEach((item, index, arr) => {
        if (item.peerId === peerId) {
          arr.splice(index, 1);
        }
      });
    },
    // 设置本端展示名称
    setDisplayName (state, displayName) {
      state.room.me.displayName = displayName;
    },
    // 设置展示的名称
    setPeerDisplayName (state, { peerId, displayName }) {
      state.room.me.consumerPeers.forEach((item, index, arr) => {
        if (item.peerId === peerId) {
          item.displayName = displayName;
        }
      });
    },
    // 设置生产者分数
    setProducerScore (state, { producerId, score }) {
      state.room.me.producers.forEach((item, index, arr) => {
        if (item.id === producerId) {
          item.score = score;
        }
      });
    },

    // 设置消费者score
    setConsumerScore (state, { consumerId, score }) {

    },
    // 设置消费者暂停
    setConsumerPaused (state, consumerId, type) {

    },
    // 移除消费者
    removeConsumer (state, { consumerId, peerId }) {
      state.room.me.consumerPeers.forEach((item, index, arr) => {
        if (item.peerId === peerId) {
          arr.splice(index, 1);
        }
      });
    },
    // 设置消费者空间层
    setConsumerCurrentLayers (state, { consumerId, spatialLayer, temporalLayer }) {

    },
    // 设置房间中激活的讲话者
    setRoomActiveSpeaker (state, { peerId }) {

    },
    // 设置消费者恢复
    setConsumerResumed (state, consumerId, type) {

    },
    // 设置摄像头改变
    setCanChangeWebcam (state, currentWebcam) {

    },
    //设置生产者暂停
    setProducerPaused (state, producerId) {

    },
    //恢复生产者
    setProducerResumed (state, producerId) {

    },
    // 设置轨道
    setProducerTrack (state, producerId, track) {
      state.room.me.producers.forEach((item, index, arr) => {
        if (item.id === producerId) {
          item.track = track;
        }
      });
    },
    //设置消费更喜欢的层
    setConsumerPreferredLayers (state, consumerId, spatialLayer, temporalLayer) {

    },
    //设置消费者优先级
    setConsumerPriority (state, consumerId, priority) {

    },
  },
  actions: {},
  modules: {},
});
